package com.example.second.iterator.classes;

import com.example.second.iterator.interfaces.Container;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @ClassName: NameRepository$
 * @Description: java类作用描述
 * @Author: xinya
 * @CreateDate: 2020/12/21$ 19:32$
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public class NameRepository implements Container<String> {
    private List<String> nameList = new ArrayList<>();

    @Override
    public Iterator<String> getIterator() {
        return new NameIterator();
    }

    public void addName(String name) {
        nameList.add(name);
    }

    public String getName(int index) {
        return nameList.get(index);
    }

    public int size() {
        return nameList.size();
    }


    private class NameIterator implements Iterator<String> {
        private int index = 0;

        @Override
        public boolean hasNext() {
            return index < nameList.size();
        }

        @Override
        public String next() {
            if (hasNext()) {
                return nameList.get(index++);
            }
            return null;
        }





    }
}
